﻿using Alipay.EasySDK.Factory;
using Alipay.EasySDK.Kernel;
using Alipay.EasySDK.Kernel.Util;
using Alipay.EasySDK.Payment.Page.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;

namespace YDT.Payment.Application
{
    /// <summary>
    /// 阿里网页支付
    /// </summary>
    public class AliPagePay : IWebPagePay
    {
        /// <summary>
        /// 创建支付
        /// </summary>
        /// <param name="productName"></param>
        /// <param name="orderSn"></param>
        /// <param name="totalPrice"></param>
        public WebPagePayResult CreatePay(string productName, string orderSn, string totalPrice)
        {
            // 1、支付配置
            Factory.SetOptions(GetConfig());
            try
            {
                // 2、支付网页创建
                AlipayTradePagePayResponse response = Factory.Payment.Page().Pay(productName, orderSn, totalPrice, "http://406286l2k9.wicp.vip");
                // 3. 支付状态判断
                if (ResponseChecker.Success(response))
                {
                    // 4、支付结果返回
                    WebPagePayResult webPagePayResult = new WebPagePayResult();
                    webPagePayResult.PayResult = response.Body;
                    return webPagePayResult;
                }
                else
                {
                    // 5、支付异常抛出
                    Console.WriteLine("调用失败，原因：" + response.Body + "，" + response.Body);
                    throw new AbpException("支付宝支付调用失败，原因：" + response.Body + "，" + response.Body);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("调用遭遇异常，原因：" + ex.Message);
                throw new AbpException("支付宝支付调用遭遇异常，原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 支付配置设置
        /// </summary>
        /// <returns></returns>
        static private Config GetConfig()
        {
            return new Config()
            {
                Protocol = "https",
                GatewayHost = "openapi.alipay.com",
                SignType = "RSA2",

                // <-- 请填写您的AppId，例如：2019091767145019 -->
                AppId = "2021002174613827",

                // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
                //MerchantPrivateKey = "MIIEowIBAAKCAQEAklvJH/X2H7QxZ8VFsZV3QZqr8MNTtN9XF6A1QLU5qEwheGNF0S/ZVmACKNvj/o4++fTAwbhCmbdrXOct4asdCzm6X5vn3M3xY7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Zw6udHisY5vJAoGBAOjSiExYBHQrWYf/MtUih3fnfdHUug4InR727+4zV6DDA8kO8Ajg77dx4+BynSRTZtfOo8wDw2am+panzxPEGfkuAPqJuXpP+Q9YXSwfLsGweVvipfPhNpXxyy/yotEXWELrjaCtNdBjZwGx6ddCcOGIplT+rTIPfaSLuyUJdHmVAoGBANZ/MI2pPa4cO+gTG2AXl5wG19RiHotqB/Zm6mA9tkRL6dP6WNKkgUFyHJ+WUgy2xUauICarbUbkpT6J404no9NIvq9NcFOKurlBNb2/fJq7MvtkWtwIT6OXiLvIrdDsKJUtThhbXy8ObLz1gI+JzLErqRnT896x9uCbACwrggvjAoGBALXtxZm6cYiBU03E6ejIOYlFd/jePKC4tGY82dGNhxzVJoEWzhRyCH+VXFbH2OlIzqU2EKJcEnWFZ20QJe0xIIapHxSnSCs+kMUf7HaMy1GI39wgKEOBKdFThs2p0PaIQmzQefQjRVjYQ+Z8KtkNTv29dRQXiikDF8/bbFqNn9ldAoGAbSX5ygxV47RgU57tbj9zGaVtzJhFXMIO3TbPizu79RMAIKsi6cVmNKxhnl+6ztdumaHfoG3ZRJry71jxPwLy3YAF8GSihRByJfTl6PF48a/SqJQPnCnqhJ+Gp6/dqxORLyAHvmM4k1RuxtTSn7gKXhmOiiMN1MOQkgit1Mwq9+sCgYEA2WUtpX9Duoq2Qn34scebs15DQGCmBGErtCyJgLVJBId4LRxngqwCxFNcuAfV6SZLvwISEP9/AIQ8MB1ml3U169+GGRRW6ydEdpQbOH1cMKih1SdYZXUZyjGKxNtyO5afuECuTJ8+Y1+NbpUefO5SWVjichr57mMxz6Ftnbf6Qp8=",
                //<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->
                // MerchantCertPath = "Payments/Alipay/Certs/appCertPublicKey_2021002174613827.crt",
                //< --请填写您的支付宝公钥证书文件路径，例如：/ foo / alipayCertPublicKey_RSA2.crt-- >
                // AlipayCertPath = "Payments/Alipay/Certs/alipayCertPublicKey_RSA2.crt",
                //< --请填写您的支付宝公钥证书文件路径，例如：/ foo / alipayCertPublicKey_RSA2.crt-- >
                // AlipayRootCertPath = "Payments/Alipay/Certs/alipayRootCert.crt",

                // 如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
                AlipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApEXC22yy3FPJ+WQR7Fh9nYsMTddNJidP7NXkyy0y4rEOXT4+mV/2+/eCWL7Wm95e4ugYS3oAhWYW9DTnePGgtWQKY7yvQkq0fsU8Icwxl2tn3AHDnOX7a0bzQJDkzJPw9zWAWQh/5s9gyTVUhJIeSvlRBex0HMOrCweyLEA/b66GZEW/Dk3djfnbc4iDwWGmSHZwyQhaJwJQG1fMK4Gg7jR5K274tiAiP8ZFOTbvS3gBok1gLZJpUzQP23cG93YYimzdZFcOD1mBXa2k1/m2WKGAAM61o9HhJMCxUBMCmTqEXQiYp/12wguA1k30tEA6rWt2EYpJB+hgKe9OYDR+oQIDAQAB",

                //可设置异步通知接收服务地址（可选）
                NotifyUrl = "http://406286l2k9.wicp.vip/AliCallback",

                //可设置AES密钥，调用AES加解密相关接口时需要（可选）
                //EncryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->"
            };
        }
    }
}
